import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import * as API from '../apis/search';
import { WeaLocaleProvider } from 'ecCom';
import * as Util from '../util';
import { Modal, message } from 'antd';

const { confirm } = Modal;
const getLabel = WeaLocaleProvider.getLabel;

export class SearchStore {
  @observable loading = false;
  @observable tabInfo = [
    // {
    //   viewcondition: 0,
    //   title: getLabel(1212,'我的文档'),
    //   editable: false,
    // },
    // {
    //   viewcondition: 1,
    //   title: getLabel(18441,'未读文档'),
    //   editable: false,
    // },
    // {
    //   viewcondition: 2,
    //   title: getLabel(30898,'全部文档'),
    //   editable: false,
    // },
  ];
  @observable viewcondition = 0;
  @observable getMost = '';
  @observable secid = '';
  @observable categoryid = '';
  @observable canCreate = false;
  @observable domKey = '';
  @observable linkRout = '';
  @observable linkUrl = '';
  @observable openNewWindow = '';
  @observable param = '';
  @observable showSearchAd = false;
  @observable selectedTreeKeys = [];
  @observable showThumbnails = false;
  @observable checkedKeys = [];
  @observable showDocShare = false;
  @observable showDocLog = false;
  @observable showConditonSave = false;
  @observable conditionMouldName = '';
  @observable logDocId = '';
  @observable shareDocId = '';
  @observable mvisible = false;
  @observable mcontent = '';
  @observable treeDropMenu = [
    {
      companyid: '1',
      name: getLabel(18030, '我的收藏'),
    },
    {
      companyid: '0',
      name: getLabel(385079, '全部目录'),
    },
  ];
  @observable treeDropMenuKey = '';
  @observable prePath = '';
  @observable prePathName = '';
  @observable showImportDummy = false;
  @observable importLoading = false;
  @observable importType = 'select';
  @observable importRightMenu = {};
  @observable importCondition = [];
  @observable importForm = new WeaForm();
  @observable defaultTreeNode = '';
  @observable isNotDefaultFav = false;
  @observable leftTree = {
  	hasLoad: false,
  	showLeft: false,
  }
  @observable portalParams={
    fromPortal: false,
    protalViewcondition: '',
  }


  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  getTabInfo = (params = {}) => {
    API.getTabInfo(params).then(action((data) => {
      this.tabInfo = data.groupinfo;
      this.loading = false;
    }));
    return this;
  }

  @action
  doCollapse = () => {
  	this.leftTree = {
  		hasLoad: true,
  		showLeft: !this.leftTree.showLeft,
  		};
  }

  addTabPane = (params) => {
    this.loading = true;
    API.addTabPane(params).then((data) => {
      data.status === 1 && this.getTabInfo();
    });
    return this;
  };

  delTabPane = (params) => {
    this.loading = true;
    API.delTabPane(params).then((data) => {
      data.status === 1 && this.getTabInfo();
    });
    return this;
  };

  getAccount = (params) => {
    this.loading = true;
    API.getAccount(params).then((data) => {
      this.canCreate = data.canCreate;
    });
    return this;
  }

  @action
  chooseDummy = (params) => {
    this.importLoading = true;
    API.chooseDummy(params).then(action((data) => {
      this.importRightMenu = data.rightMenu;
      this.importCondition = data.condition;
      this.importForm.initFormFields(data.condition);
      this.importLoading = false;
    }));
    return this;
  }

  @action
  importDummy = (params) => {
    this.importLoading = true;
    API.importDummy(params).then(action(() => {
      this.showImportDummy = false;
      this.importForm.reset();
      this.importLoading = false;
      message.success(getLabel(25750, '导入成功'));
    }));
    return this;
  }

  markReaded = (params, callback) => {
    this.loading = true;
    API.markReaded(params).then(() => {
      callback();
      this.loading = false;
    });
    return this;
  }

  @action
  getDefaultTree = (params = {}) => API.getDefaultTree(params).then(action((result) => {
      this.defaultTreeNode = result.default_treenode;
      this.treeDropMenuKey = this.defaultTreeNode;
      this.portalParams = {
        fromPortal: result.fromPortal,
        protalViewcondition: result.protalViewcondition,
      };
      if (result.showLeftTree) {
      		this.leftTree = {
			  	hasLoad: false,
			  	showLeft: true,
			  };
      }
    }))
  @action
  installLeftTree = () => {
  	this.leftTree = {
			  	hasLoad: false,
			  	showLeft: false,
			  };
  }

  @action
  createUrl = () => {
    API.createUrl().then(action((data) => {
      this.domKey = data.domKey;
      this.linkRout = data.linkRout;
      this.linkUrl = data.linkUrl;
      this.openNewWindow = data.openNewWindow;
      this.param = data.params;
    }));
  }
  createDoc = (id) => {
    if (id === '' || id === null) {
      Modal.confirm({
        title: getLabel(131329, '信息确认'),
        content: getLabel(390715, ' 您还没有选择任何目录!'),
        onOk: () => {
         this.canCreate = false;
        },
      });
    } else {
      const url = `${this.linkUrl}?${this.domKey}=${
       id}${this.param}${this.linkRout}`;
        if (this.openNewWindow === '1') {
          window.open(url);
        } else {
          window.location.href = url;
        }
     }
  }
}
